package com.pay.common.util;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;


@Component
public class OssUploadUtil {
//    // endpoint是访问OSS的域名。如果您已经在OSS的控制台上 创建了Bucket，请在控制台上查看域名。
//    // 如果您还没有创建Bucket，endpoint选择请参看文档中心的“开发人员指南 > 基本概念 > 访问域名”，
//    // 链接地址是：https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/endpoint.html?spm=5176.docoss/user_guide/endpoint_region
//    // endpoint的格式形如“http://oss-cn-hangzhou.aliyuncs.com/”，注意http://后不带bucket名称，
//    // 比如“http://bucket-name.oss-cn-hangzhou.aliyuncs.com”，是错误的endpoint，请去掉其中的“bucket-name”。
//    //http://oss-cn-hangzhou-internal.aliyuncs.com
//    @Value("${oss.endpoint}")
//    private String endpoint;
//
//    // accessKeyId和accessKeySecret是OSS的访问密钥，您可以在控制台上创建和查看，
//    // 创建和查看访问密钥的链接地址是：https://ak-console.aliyun.com/#/。
//    // 注意：accessKeyId和accessKeySecret前后都没有空格，从控制台复制时请检查并去除多余的空格。
//    //LTAI0SGORGYGadLl
//    @Value("${oss.access-key-id}")
//    private String accessKeyId;
//    //VoUS4FEmqlcRKNSg1qzT3YOrtJGZ76
//    @Value("${oss.access-key-secret}")
//    private String accessKeySecret;
//
//    // Bucket用来管理所存储Object的存储空间，详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
//    // Bucket命名规范如下：只能包括小写字母，数字和短横线（-），必须以小写字母或者数字开头，长度必须在3-63字节之间。
//    //lanbeixzj
//    @Value("${oss.bucket-name}")
//    private String bucketName;
//    //private  String bucketName = "lanbeixzj-product"; //生产
//
//    // Object是OSS存储数据的基本单元，称为OSS的对象，也被称为OSS的文件。详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
//    // Object命名规范如下：使用UTF-8编码，长度必须在1-1023字节之间，不能以“/”或者“\”字符开头。
//    //private  String key = "my-first-key";
//    public String imgServiceUrl = "http://xzj.muyijinfu.com"; //测试
//
//    //public  String imgServiceUrl = "http://lanbeixzj.muyijinfu.com";//生产
//
//
//
//
//
//    public boolean uploadImg(InputStream source, String originalFilename) {
//        if (originalFilename.startsWith("/")) {
//            originalFilename = originalFilename.substring(1, originalFilename.length());
//        }
//        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//        try {
//
//            /*
//             * Determine whether the bucket exists
//             */
//            if (!ossClient.doesBucketExist(bucketName)) {
//                System.out.println("bucketName不存在");
//                /*
//                 * Create a new OSS bucket
//                 */
//               /* System.out.println("Creating bucket " + bucketName + "\n");
//                ossClient.createBucket(bucketName);
//                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
//                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
//                ossClient.createBucket(createBucketRequest);*/
//            }
//
//            ossClient.putObject(bucketName, originalFilename, source);
//            System.out.println("filekey==" + originalFilename);
//            return  true;
//        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//            System.out.println("Error Message: " + oe.getErrorCode());
//            System.out.println("Error Code:       " + oe.getErrorCode());
//            System.out.println("Request ID:      " + oe.getRequestId());
//            System.out.println("Host ID:           " + oe.getHostId());
//            return false;
//        } catch (ClientException ce) {
//            System.out.println("Caught an ClientException, which means the client encountered "
//                    + "a serious internal problem while trying to communicate with OSS, "
//                    + "such as not being able to access the network.");
//            System.out.println("Error Message: " + ce.getMessage());
//        } finally {
//            ossClient.shutdown();
//        }
//        return false;
//    }
//
//    public boolean uploadImg(byte[] source, String originalFilename) {
//        return uploadImg(new ByteArrayInputStream(source), originalFilename);
//    }
//
//    public URL getURL(String key) {
//        if (key.startsWith("/")) {
//            key = key.substring(1, key.length());
//        }
//        OSSClient Server = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET);
//        //设置过期时间 30秒过期
//        long curren = System.currentTimeMillis();
//        curren += 0.5 * 60 * 1000;
//        Date da = new Date(curren);
//        request.setExpiration(da);
//        // 生成URL签名(HTTP GET请求)
//        System.out.println("访问地址====" + Server.generatePresignedUrl(request));
//
//        return Server.generatePresignedUrl(request);
//    }
//
//    public byte[] download(String key) {
//        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//        if (key.startsWith("/")) {
//            key = key.substring(1, key.length());
//        }
//        OSSObject ossObject = ossClient.getObject(bucketName, key);
//        byte[] buf = new byte[1024];
//        try {
//            InputStream in = ossObject.getObjectContent();
//            for (int n = 0; n != -1; ) {
//                n = in.read(buf, 0, buf.length);
//            }
//            in.close();
//
//            //    System.out.println("Object：" + key + "的内容是：" + objectContent);
//        } catch (OSSException oe) {
//            oe.printStackTrace();
//        } catch (ClientException ce) {
//            ce.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            ossClient.shutdown();
//        }
//        return buf;
//    }
//
////    endpoint: oss-cn-hangzhou.aliyuncs.com
////    access-key-id: LTAI0SGORGYGadLl
////    access-key-secret: VoUS4FEmqlcRKNSg1qzT3YOrtJGZ76
////    bucket-name: lanbeicloud-test
////    bucket-name.signature: lanbei-signature-test
////    signature:
////    imgServiceUrl: https://signosstest.muyijinfu.com
//
//
////    oss.endpoint=oss-cn-hangzhou.aliyuncs.com
////    oss.access-key-id=LTAI0SGORGYGadLl
////    oss.access-key-secret=VoUS4FEmqlcRKNSg1qzT3YOrtJGZ76
////    oss.bucket-name=lanbeicloud
////    oss.bucket-name.signature = lanbei-signature
////    oss.signature.imgServiceUrl = https://signoss.muyijinfu.com
//
//
////    public static void main(String[] args) throws Exception {
////        OSSClient ossClient = new OSSClient("oss-cn-hangzhou.aliyuncs.com", "LTAI0SGORGYGadLl", "VoUS4FEmqlcRKNSg1qzT3YOrtJGZ76");
////        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("lanbeioss", "test_electronic_receipt/20190409/LACCP2019040114301633887060_66236/2019040171009184.png", HttpMethod.GET);
////        //设置过期时间
////        long curren = System.currentTimeMillis();
////        curren += 10 * 60 * 1000;
////        Date da = new Date(curren);
////        request.setExpiration(da);
////        // 生成URL签名(HTTP GET请求)
////        System.out.println("访问地址====" + ossClient.generatePresignedUrl(request));
////        ossClient.generatePresignedUrl(request);
////    }
//
//
//    public InputStream downloadin(String key) {
//        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//        OSSObject ossObject = ossClient.getObject(bucketName, key);
//        byte[] buf = new byte[1024];
//        try {
//            InputStream in = ossObject.getObjectContent();
//            return in;
//
//            //    System.out.println("Object：" + key + "的内容是：" + objectContent);
//        } catch (OSSException oe) {
//            oe.printStackTrace();
//        } catch (ClientException ce) {
//            ce.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            ossClient.shutdown();
//        }
//        return null;
//    }

}
